import AppLayout from '@/layout/AppLayout.vue';
import { createRouter, createWebHistory } from 'vue-router';
console.log(import.meta.env.BASE_URL);
const router = createRouter({
    history: createWebHistory('/questionBankManage/'),
    routes: [
        {
            path: '/',
            component: AppLayout,
            children: [
                {
                    path: '/',
                    name: 'dashboard',
                    component: () => import('@/views/Dashboard.vue'),
                    meta: {
                        requiresAuth: true
                    }
                },

                {
                    path: '/questionBankCategory',
                    name: 'questionBankCategory',
                    component: () => import('@/views/app/questionBank/QuestionBankCategory.vue')
                },
                {
                    path: '/questionBank',
                    name: 'questionBank',
                    component: () => import('@/views/app/questionBank/QuestionBank.vue')
                },
                {
                    path: '/question',
                    name: 'question',
                    component: () => import('@/views/app/questionBank/Question.vue')
                },
                {
                    path: '/question/:id',
                    name: 'QuestionDetail',
                    component: () => import('@/views/app/questionBank/QuestionDetail.vue')
                },
                {
                    path: '/question/new',
                    name: 'QuestionNew',
                    component: () => import('@/views/app/questionBank/QuestionDetail.vue')
                },
                {
                    path: '/questionExamParams',
                    name: 'QuestionExamParams',
                    component: () => import('@/views/app/questionBank/QuestionExamParam.vue')
                },
                {
                    path: '/activationCode',
                    name: 'activationCode',
                    component: () => import('@/views/app/questionBank/ActivationCode.vue')
                },
                {
                    path: '/generateActivationCode',
                    name: 'generateActivationCode',
                    component: () => import('@/views/app/questionBank/GenerateActivationCode.vue')
                },
                {
                    path: '/activationCodeUsage',
                    name: 'activationCodeUsage',
                    component: () => import('@/views/app/questionBank/ActivationCodeUsage.vue')
                },
                {
                    path: '/bankQuestionRelation',
                    name: 'bankQuestionRelation',
                    component: () => import('@/views/app/questionBank/BankUserRelation.vue')
                },
                {
                    path: '/userinfo',
                    name: 'userinfo',
                    component: () => import('@/views/app/questionBank/User.vue')
                }
            ]
        },




        {
            path: '/pages/notfound',
            name: 'notfound',
            component: () => import('@/views/pages/NotFound.vue')
        },

        {
            path: '/auth/login',
            name: 'login',
            component: () => import('@/views/pages/auth/Login.vue'),
            meta: {
                requiresAuth: false
            }
        },
        {
            path: '/auth/access',
            name: 'accessDenied',
            component: () => import('@/views/pages/auth/Access.vue')
        },
        {
            path: '/auth/error',
            name: 'error',
            component: () => import('@/views/pages/auth/Error.vue')
        }
    ],
    scrollBehavior() {
        return { left: 0, top: 0 };
    }
});

router.beforeEach((to, from, next) => {
    const isAuthenticated = localStorage.getItem('auth-token');

    if (to.meta.requiresAuth && !isAuthenticated) {
        next('/auth/login');
    }
    else if (isAuthenticated && to.path === '/auth/login') {
        next('/');
    }
    else {
        next();
    }
});

export default router;
